import { login, logout } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
const state = {
  token: getToken(),
  loaded: false,
  name: '',
  avatar: '',
  roles: [],
  positions: [],
  authorities: [],
  limitdatas: {},
  info: null
}

const mutations = {
  SET_LOADED: (state, loaded) => {
    state.loaded = loaded
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_INFO: (state, info) => {
    state.info = info
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_POSITIONS: (state, positions) => {
    state.positions = positions
  },
  SET_AUTHORITIES: (state, authorities) => {
    state.authorities = authorities
  },
  SET_LIMITDATAS: (state, limitdatas) => {
    state.limitdatas = limitdatas
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password, code, uuid } = userInfo
    return new Promise((resolve, reject) => {
      login(username, password, code, uuid).then(response => {
        commit('SET_TOKEN', response)
        setToken(response) // save in cookie
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state, dispatch }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        commit('SET_LOADED', false)
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        commit('SET_AUTHORITIES', [])
        commit('SET_INFO', null)
        commit('SET_AVATAR', null)
        commit('SET_LIMITDATAS', null)
        removeToken()
        resetRouter()

        // reset visited views and cached views
        // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
        dispatch('tagsView/delAllViews', null, { root: true })

        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_LOADED', false)
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      commit('SET_POSITIONS', [])
      commit('SET_AUTHORITIES', [])
      commit('SET_INFO', null)
      commit('SET_AVATAR', null)
      commit('SET_LIMITDATAS', null)
      removeToken()
      resolve()
    })
  },
  /**
   * roles 保存用户色
   * authorities 保存用户授权
   */
  setUserInfo({ commit }, res) {
    return new Promise(resolve => {
      commit('SET_LOADED', true)
      commit('SET_ROLES', res.roles)
      commit('SET_POSITIONS', res.positions)
      commit('SET_AUTHORITIES', res.authorities)
      commit('SET_NAME', res.user.name)
      commit('SET_AVATAR', res.user.avatar)
      commit('SET_INFO', res.user)
      resolve()
    })
  },
  /**
   * 用户的数据权限，已按组件要求，组织成树或数组
   */
  setUserLimitDatas({ commit }, limitdatas) {
    return new Promise(resolve => {
      commit('SET_LIMITDATAS', limitdatas)
      resolve()
    })
  },

  setUserAvatar({ commit }, avatar) {
    return new Promise(resolve => {
      commit('SET_AVATAR', avatar)
      resolve()
    })
  },

  updateUserInfo({ commit }, userInfo) {
    return new Promise(resolve => {
      commit('SET_INFO', userInfo)
      resolve()
    })
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
